import { NestFactory } from '@nestjs/core';
import { VersioningType, ValidationPipe } from '@nestjs/common';
import { NestExpressApplication } from '@nestjs/platform-express';
import { NextFunction, Request, Response } from 'express';
import { I18nValidationExceptionFilter, I18nValidationPipe } from 'nestjs-i18n';
import { AppModule } from './app.module';
import { WINSTON_LOGGER_TOKEN } from './modules/logger/logger.module';
import { LoginGuard } from './guard/login.guard';
import { TimeInterceptor } from './interceptor/time.interceptor';
import { ValidatePipe } from './pipe/validate.pipe';
import { TestFilter } from './filter/test.filter';

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule, {
    cors: true,
  });

  app.useLogger(app.get(WINSTON_LOGGER_TOKEN));

  app.useStaticAssets('public', { prefix: '/static' });

  app.use(function (req: Request, res: Response, next: NextFunction) {
    next();
  });

  // 开启多接口版本
  app.enableVersioning({
    // 以请求头字段决定版本
    // type: VersioningType.HEADER,
    // header: 'version'

    // 以 URI 决定版本
    type: VersioningType.URI,
  });

  app.useGlobalPipes(
    new ValidationPipe({
      transform: true,
    }),
  );

  // app.useGlobalPipes(new I18nValidationPipe());

  // app.useGlobalFilters(
  //   new I18nValidationExceptionFilter({
  //     detailedErrors: false,
  //   }),
  // );

  // app.useGlobalGuards(new LoginGuard())

  // app.useGlobalInterceptors(new TimeInterceptor())

  // app.useGlobalFilters(new TestFilter())

  await app.listen(3000);
}
bootstrap();
